﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;
using System.IO;

namespace ZG_member
{
    public partial class CardTemplateForm : Form
    {
        public CardTemplateForm(CardTemplateDesignMainForm tm)
        {
            InitializeComponent();
            height = this.Height;
            width = this.Width;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            //this.Height = 0; 
            //this.Width = 0;
            tm2 = tm;
            //Label ll = new Label();
            //ll.BackColor = Color.Transparent;
            //ll.Name = "templateString3";
            //ll.Height = 250;
            //ll.Width = 300;
            //ll.Top = 65;
            //ll.Left = 71;

            //this.pictureBox1.Controls.Add(ll);
            ////双排
            //Label right = new Label();
            //right.BackColor = Color.Transparent;
            //right.Name = "templateStringright";
            //right.Height = 84;
            //right.Width = 150;
            //right.Top = 65;
            //right.Left = 221;

            //this.pictureBox1.Controls.Add(right);



            Label ll2 = new Label();
            ll2.BackColor = Color.Transparent;
            ll2.Name = "templateString4";
            ll2.Height = 250;
            ll2.Width = 150;
            ll2.Top = 70;
            ll2.Left = 40;
            this.pictureBox2.Controls.Add(ll2);

            ////双排
            //Label right2 = new Label();
            //right2.BackColor = Color.Transparent;
            //right2.Name = "templateStringright2";
            //right2.Height = 250;
            //right2.Width = 150;
            //right2.Top = 70;
            //right2.Left = 221;
            //this.pictureBox2.Controls.Add(right2);

        }
        CardTemplateDesignMainForm tm2;
        int height;
        int width;
        public static int ids;
        protected string GetTemplateString()
        {
            string templateString = string.Empty;
            string temp = "\n";

            for (int i = 0; i < alreadyAddItem.Items.Count; i++)
            {
                templateString += alreadyAddItem.Items[i].ToString();
                if (alreadyAddItem.Items.Count - 1 != i)
                {
                    templateString += temp;
                }

            }

            return templateString;

        }
        //protected string GetTemplateString1()
        //{
        //    string templateString = string.Empty;
        //    string temp = "\n";

        //    for (int i = 0; i < alreadyAddItem1.Items.Count; i++)
        //    {
        //        templateString += alreadyAddItem1.Items[i].ToString();
        //        if (alreadyAddItem1.Items.Count - 1 != i)
        //        {
        //            templateString += temp;
        //        }

        //    }
        //    return templateString;
        //}


        private void btnTop_Click(object sender, EventArgs e)
        {
            if (alreadyAddItem.SelectedItem != null)
            {
                object selectedItem = alreadyAddItem.SelectedItem;
                alreadyAddItem.Items.RemoveAt(alreadyAddItem.SelectedIndex);
                alreadyAddItem.Items.Insert(0, selectedItem);
                alreadyAddItem.SelectedIndex = 0;
                if (moban == 1)
                {
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                }
                else
                {
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                    //templateString2.Text = GetTemplateString();
                }
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (alreadyAddItem.SelectedItem != null && alreadyAddItem.SelectedIndex != 0)
            {
                object selectedItem = alreadyAddItem.SelectedItem;
                int selectedIndex = alreadyAddItem.SelectedIndex;
                alreadyAddItem.Items.RemoveAt(selectedIndex);
                alreadyAddItem.Items.Insert(selectedIndex - 1, selectedItem);
                alreadyAddItem.SelectedIndex = selectedIndex - 1;
                if (moban == 1)
                {
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                }
                else
                {
                    //templateString2.Text = GetTemplateString();
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                }
            }
        }

        private void btndown_Click(object sender, EventArgs e)
        {
            if (alreadyAddItem.SelectedItem != null && alreadyAddItem.SelectedIndex != alreadyAddItem.Items.Count - 1)
            {
                object selectedItem = alreadyAddItem.SelectedItem;
                int selectedIndex = alreadyAddItem.SelectedIndex;
                alreadyAddItem.Items.RemoveAt(selectedIndex);
                alreadyAddItem.Items.Insert(selectedIndex + 1, selectedItem);
                alreadyAddItem.SelectedIndex = selectedIndex + 1;
                if (moban == 1)
                {
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                }
                else
                {
                    //templateString2.Text = GetTemplateString();
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                }
            }
        }

        private void btnButtom_Click(object sender, EventArgs e)
        {
            if (alreadyAddItem.SelectedItem != null)
            {
                object selectedItem = alreadyAddItem.SelectedItem;
                alreadyAddItem.Items.RemoveAt(alreadyAddItem.SelectedIndex);
                alreadyAddItem.Items.Add(selectedItem);
                alreadyAddItem.SelectedIndex = alreadyAddItem.Items.Count - 1;
                if (moban == 1)
                {
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                }
                else
                {
                    //templateString2.Text = GetTemplateString();
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            item.Text = GetTemplateString();
                        }
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < items.SelectedItems.Count; i++)
            {
                alreadyAddItem.Items.Add(items.SelectedItems[i]);
            }
            if (moban == 1)
            {
                foreach (Control item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
            else
            {
                //templateString2.Text = GetTemplateString();
                foreach (Control item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }

        }

        private void btnAdd1_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < advItems.SelectedItems.Count; i++)
            {
                alreadyAddItem.Items.Add(AdvPreProcess(advItems.SelectedItems[i].ToString()));

            }
            if (moban == 0)
            {
                foreach (Control item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
            else
            {
                //templateString2.Text = GetTemplateString();
                foreach (Control item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
        }
        private string AdvPreProcess(string advContent)
        {
            string processContent = string.Empty;
            int count = 0;

            for (int i = 0; i < advContent.Length; i++)
            {
                processContent += advContent[i].ToString();

                if (advContent[i] == '\n')
                {
                    count = 0;
                    continue;
                }

                count++;

                if (advContent[i] < 0 || advContent[i] > 255)
                {
                    count++;
                }
                if (moban == 0)
                {
                    if (count >= 24)
                    {
                        if (advContent.Length >= i + 2)
                        {
                            if (advContent[i + 1] != '\n')
                            {
                                processContent += "\n";
                                count = 0;
                            }
                        }
                    }
                }
                else if (moban == 1)
                {
                    if (count >= 44)
                    {
                        if (advContent.Length >= i + 2)
                        {
                            if (advContent[i + 1] != '\n')
                            {
                                processContent += "\n";
                                count = 0;
                            }
                        }
                    }
                }

            }

            return processContent;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (moban == 0)
            {
                cardTemplates card = new cardTemplates();
                Model.cardTemplates cardModel = new Model.cardTemplates();
                if (this.Text == "修改模板")
                {
                    cardModel.id = ids;
                    cardModel.name = this.templateName.Text;
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright")
                        //{
                        //    if (item.Text!= "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }

                    if (card.Update(cardModel))
                    {
                        MessageBox.Show("修改成功");
                    }


                }
                else
                {
                    if (templateName.Text == "")
                    {
                        MessageBox.Show("模板名不能为空!"); return;
                    }
                    cardModel.name = this.templateName.Text;
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright")
                        //{
                        //    if (item.Text != "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }
                    //cardModel.template = this.templateString.Text;
                    int row = card.Add(cardModel);
                    if (row > 0)
                    {
                        MessageBox.Show("添加成功!");
                    }
                }
            }
            else if (moban == 1)
            {
                cardTemplates card = new cardTemplates();
                Model.cardTemplates cardModel = new Model.cardTemplates();
                if (this.Text == "修改模板")
                {
                    cardModel.id = ids;
                    cardModel.name = this.templateName.Text;
                    //cardModel.template = this.templateString2.Text;
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright2")
                        //{
                        //    if (item.Text != "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }
                    if (card.Update(cardModel))
                    {
                        MessageBox.Show("修改成功");
                    }

                }
                else
                {
                    if (templateName.Text == "")
                    {
                        MessageBox.Show("模板名不能为空!"); return;
                    }
                    cardModel.name = this.templateName.Text;
                    //cardModel.template = this.templateString2.Text;
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright2")
                        //{
                        //    if (item.Text != "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }
                    int row = card.Add(cardModel);
                    if (row > 0)
                    {
                        MessageBox.Show("添加成功!");
                    }
                }
            }
            CardTemplateDesignMainForm cardForm = new CardTemplateDesignMainForm();
            cardTemplates cards = new cardTemplates();
            tm2.cardTemplatesGdv.DataSource = cards.GetList("").Tables[0];
        }
        int moban = 0;
        private void CardTemplateForm_Load(object sender, EventArgs e)
        {
            Label ll = new Label();
            ll.BackColor = Color.Transparent;
            ll.Name = "templateString3";
            ll.Height = 250;
            ll.Width = 300;
            ll.Top = 65;
            ll.Left = 71;
            this.pictureBox1.Controls.Add(ll);
            Label ll2 = new Label();
            ll2.BackColor = Color.Transparent;
            ll2.Name = "templateString4";
            ll2.Height = 250;
            ll2.Width = 300;
            ll2.Top = 70;
            ll2.Left = 40;
            this.pictureBox2.Controls.Add(ll2);
            string ImagePath = "";
            StreamReader sr = new StreamReader("ImagePath.ini", Encoding.GetEncoding("gb2312"));
            if (sr.Peek() >= 0)
            {
                ImagePath = sr.ReadLine();
            }
            else
            {
            }
            int num = 0;
            //foreach (Label item in pictureBox1.Controls)
            //{
            //    if (item.Name == "templateString3")
            //    {
            //        item.Text = "累计积分                可用积分                本次消费                本次积分                储值余额                办卡日期                下一等级                \n\n";
            //        num = 1;
            //    }
            //}
            //if (num == 0)
            //{
            //    foreach (Label item in pictureBox2.Controls)
            //    {
            //        if (item.Name == "templateString4")
            //        {
            //            item.Text = "累计积分           可用积分           本次消费           本次积分           储值余额           办卡日期           下一等级           \n\n";
            //            num = 1;
            //        }
            //    }
            //}
            if (ImagePath.Trim() != "")
            {

                this.pictureBox1.ImageLocation = ImagePath;
                this.pictureBox2.ImageLocation = ImagePath;
            }
            sr.Close();
            for (int i = 0; i < tm2.dgvadvinfo.Rows.Count; i++)
            {
                if (tm2.dgvadvinfo.Rows[i].Cells["advContent"].Value != "")
                {
                    advItems.Items.Add(tm2.dgvadvinfo.Rows[i].Cells["advContent"].Value);
                }
            }

            moban = int.Parse(ConfigurationManager.AppSettings["direction"]);
            this.timer1.Start();
            // TODO: 这行代码将数据加载到表“zG_MemberDataSet.advInfo”中。您可以根据需要移动或删除它。
            //this.advInfoTableAdapter.Fill(this.zG_MemberDataSet.advInfo);

            if (moban == 1)
            {
                this.Plhen.Visible = true;
                this.Plshu.Visible = false;
                //this.templateString2.Visible = false;
                foreach (Label item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Visible = false; ;
                    }
                }
                foreach (Label item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
            else if (moban == 0)
            {
                this.Plshu.Visible = true;
                this.Plhen.Visible = false;
                foreach (Label item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Visible = false;
                    }
                }
                //templateString2.Text = GetTemplateString();
                foreach (Label item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }


        }

        private void btnremove_Click(object sender, EventArgs e)
        {

            int a = alreadyAddItem.SelectedIndex;
            if (a == -1)
            {
                MessageBox.Show("请选择要删除的项目名！");
                return;
            }
            alreadyAddItem.Items.RemoveAt(a);

            if (moban == 1)
            {
                //templateString.Text = GetTemplateString();
                foreach (Control item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
            else
            {
                //templateString2.Text = GetTemplateString();
                foreach (Control item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void CardTemplateForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void alreadyAddItem_DoubleClick(object sender, EventArgs e)
        {
            int a = alreadyAddItem.SelectedIndex;
            if (a < 0)
            {
                MessageBox.Show("请选择要删除的项目！");
                return;
            }
            alreadyAddItem.Items.RemoveAt(a);

            if (moban == 1)
            {
                foreach (Control item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Text = GetTemplateString();
                    }
                    //if (item.Name == "templateStringright")
                    //{
                    //    item.Text = GetTemplateString();
                    //}
                }
            }
            else
            {
                //templateString2.Text = GetTemplateString();
                foreach (Control item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Text = GetTemplateString();
                    }
                    //if (item.Name == "templateStringright2")
                    //{
                    //    item.Text = GetTemplateString();
                    //}
                }
            }
        }

        private void items_DoubleClick(object sender, EventArgs e)
        {
            for (int i = 0; i < items.SelectedItems.Count; i++)
            {
                alreadyAddItem.Items.Add(items.SelectedItems[i]);
            }
            if (moban == 1)
            {
                foreach (Control item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
            else
            {
                //templateString2.Text = GetTemplateString();
                foreach (Control item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
        }

        private void advItems_DoubleClick(object sender, EventArgs e)
        {

            for (int i = 0; i < advItems.SelectedItems.Count; i++)
            {
                alreadyAddItem.Items.Add(AdvPreProcess(advItems.SelectedItems[i].ToString()));

            }
            if (moban == 1)
            {
                foreach (Control item in pictureBox1.Controls)
                {
                    if (item.Name == "templateString3")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
            else
            {
                //templateString2.Text = GetTemplateString();
                foreach (Control item in pictureBox2.Controls)
                {
                    if (item.Name == "templateString4")
                    {
                        item.Text = GetTemplateString();
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (moban == 1)
            {
                cardTemplates card = new cardTemplates();
                Model.cardTemplates cardModel = new Model.cardTemplates();
                if (this.Text == "修改模板")
                {
                    cardModel.id = ids;
                    cardModel.name = this.templateName.Text;
                    //cardModel.template = this.templateString.Text;
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright")
                        //{
                        //    if (item.Text != "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }
                    if (card.Update(cardModel))
                    {
                        MessageBox.Show("修改成功");
                    }

                }
                else
                {
                    if (templateName.Text == "")
                    {
                        MessageBox.Show("模板名不能为空!"); return;
                    }
                    cardModel.name = this.templateName.Text;
                    //cardModel.template = this.templateString.Text;
                    foreach (Control item in pictureBox1.Controls)
                    {
                        if (item.Name == "templateString3")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright")
                        //{
                        //    if (item.Text != "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }
                    int row = card.Add(cardModel);
                    if (row > 0)
                    {
                        MessageBox.Show("添加成功!");
                    }
                }
                this.Close();
            }
            else if (moban == 0)
            {
                cardTemplates card = new cardTemplates();
                Model.cardTemplates cardModel = new Model.cardTemplates();
                if (this.Text == "修改模板")
                {
                    cardModel.id = ids;
                    cardModel.name = this.templateName.Text;
                    //cardModel.template = this.templateString2.Text;
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright2")
                        //{
                        //    if (item.Text != "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }
                    if (card.Update(cardModel))
                    {
                        MessageBox.Show("修改成功");
                    }

                }
                else
                {
                    if (templateName.Text == "")
                    {
                        MessageBox.Show("模板名不能为空!"); return;
                    }
                    cardModel.name = this.templateName.Text;
                    //cardModel.template = this.templateString2.Text;
                    foreach (Control item in pictureBox2.Controls)
                    {
                        if (item.Name == "templateString4")
                        {
                            cardModel.template = item.Text;
                        }
                        //if (item.Name == "templateStringright2")
                        //{
                        //    if (item.Text != "" && item.Text != null)
                        //    {
                        //        cardModel.template += "#";
                        //        cardModel.template += item.Text;
                        //    }
                        //}
                    }
                    int row = card.Add(cardModel);
                    if (row > 0)
                    {
                        MessageBox.Show("添加成功!");
                    }
                }
                this.Close();
            }
            CardTemplateDesignMainForm cardForm = new CardTemplateDesignMainForm();
            cardTemplates cards = new cardTemplates();
            tm2.cardTemplatesGdv.DataSource = cards.GetList("").Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.ShowDialog();
            if (fd.FileName != "")
            {

                StreamWriter sw = new StreamWriter("ImagePath.ini", false, Encoding.GetEncoding("gb2312"));
                sw.Flush();
                sw.Write(fd.FileName);
                sw.Close();

                string ImagePath = "";
                StreamReader sr = new StreamReader("ImagePath.ini", Encoding.GetEncoding("gb2312"));
                if (sr.Peek() >= 0)
                {
                    ImagePath = sr.ReadLine();
                }
                else
                {
                }

                if (ImagePath.Trim() != "")
                {

                    this.pictureBox1.ImageLocation = ImagePath;
                    this.pictureBox2.ImageLocation = ImagePath;
                }
                sr.Close();

                MessageBox.Show("导图成功");
            }
        }














    }
}
